package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.base.CommonMethods;
import com.domain.Page;
import com.domain.SMSInfo;
import com.domain.User;
import com.service.impl.SMSServiceImpl;

@Component
@Controller
public class SMSController {

	@Autowired
	// 自动注入
	private SMSServiceImpl smsService;

	@RequestMapping(method = RequestMethod.GET, value = "/getSMS")
	public void getSMS(HttpServletRequest request,
			HttpServletResponse response, Page page, String teamText,
			String conText, String NoText, String cdate1, String cdate2,
			String sender) throws Exception {
		try {
			teamText = new String(teamText.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			conText = new String(conText.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			NoText = new String(NoText.getBytes("ISO-8859-1"), "utf-8");

		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			teamText = new String(teamText.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			// TODO: handle exception
		}

		if (page == null) {
			page = new Page(0, 0, 15);
		}
		System.out.println("teamText:" + teamText);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		System.out.println("cdate2" + cdate2);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (CommonMethods.isEmpty(cdate1) && CommonMethods.isEmpty(cdate2)) {
			long temp = new Date().getTime() - 7 * 24 * 60 * 60 * 1000;
			cdate1 = sdf.format(temp);
			Date date = sdf.parse(sdf.format(new Date()));
			temp = date.getTime() + 24 * 60 * 60 * 1000;
			cdate2 = sdf.format(temp);
		} else if (!CommonMethods.isEmpty(cdate2)) {
			cdate2 = cdate2.substring(0, 10);
			Date date = sdf.parse(cdate2);
			long temp = date.getTime() + 24 * 60 * 60 * 1000;
			cdate2 = sdf.format(temp);
		}
		String result = smsService.getSMSInfo(page, user, teamText, conText,
				NoText, cdate1, cdate2, sender);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();
		System.out.println("查询到的数据为：" + result);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getAddress")
	public void getAddress(HttpServletRequest request,
			HttpServletResponse response, String isAll) throws IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			response.sendRedirect("login.html");
			return;
		}
		int flag = 0;
		if (isAll != null || isAll != "") {
			flag = Integer.parseInt(isAll);
		}
		String result = smsService.getAddress(user, flag);
		System.out.println("result:" + result);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getModel")
	public void getModel(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			response.sendRedirect("login.html");
			return;
		}
		String result = smsService.getModel();
		System.out.println("result:" + result);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getAddressInfo")
	public void getAddressInfo(HttpServletRequest request,
			HttpServletResponse response, String id) throws IOException,
			SQLException {
		System.out.println("进入getAddressInfo方法！id:" + id);
		if (id == null) {
			return;
		}
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			response.sendRedirect("login.html");
			return;
		}
		String result = smsService.getAddressInfo(id, user);
		result = result.replace(",", "\n");
		System.out.println("计算结果：" + result);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();
	}

	@RequestMapping(method = RequestMethod.POST, value = "/sendSMS")
	public void sendSMS(HttpServletRequest request,
			HttpServletResponse response, String teamId, String teamName,
			String mes) throws IOException, SQLException {

		if (teamId == null || mes == null) {
			return;
		}
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return;
		}
		System.out.println("team:" + teamId + "mes:" + mes);
		boolean result = smsService.sendSMS(teamId, teamName, mes, user);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/delSMS")
	public void delSMS(HttpServletRequest request,
			HttpServletResponse response, String id) {
		if (id == null) {
			return;
		}
		boolean result = smsService.delSMSInfo(id);
		// response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		write.close();
	}

	/**
	 * 获取短信模版
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getSMSModel")
	public void getSMSModel(HttpServletRequest request,
			HttpServletResponse response, Page page) throws Exception {
		System.out.println("进入这个方法。。。" + page);

		if (page == null) {
			page = new Page(0, 0, 15);
		}
		String result = smsService.getSMSModel(page);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();

	}

	/**
	 * 删除短信模版
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/delModel")
	public void delModel(HttpServletRequest request,
			HttpServletResponse response, String id) {
		System.out.println("id:" + id);
		if (id == null) {
			return;
		}
		boolean result = smsService.delSMSModel(id);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		write.close();
	}

	/**
	 * 增加短信模版
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/addSMSModel")
	public void addSMSModel(HttpServletRequest request,
			HttpServletResponse response, String nameText, String contentText)
			throws Exception {
		nameText = new String(nameText.getBytes("ISO-8859-1"), "utf-8");
		contentText = new String(contentText.getBytes("ISO-8859-1"), "utf-8");
		System.out.println(".isValid():" + nameText + "contentText:"
				+ contentText);
		response.setContentType("text/html; charset=utf-8");
		boolean result = smsService.addSMSModel(nameText, contentText);
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/editSMSModel")
	public void editSMSModel(HttpServletRequest request,
			HttpServletResponse response, String id, String nameText,
			String contentText) throws Exception {
		nameText = new String(nameText.getBytes("ISO-8859-1"), "utf-8");
		contentText = new String(contentText.getBytes("ISO-8859-1"), "utf-8");
		System.out.println(".isValid():" + nameText + "contentText:"
				+ contentText);
		response.setContentType("text/html; charset=utf-8");
		boolean result = smsService.editSMSModel(id, nameText, contentText);
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getBook")
	public void getBook(HttpServletRequest request,
			HttpServletResponse response, Page page, int team, int classTeam)
			throws Exception {
		System.out.println("team:" + team + "classTeam:" + classTeam);
		if (page == null) {
			page = new Page(0, 0, 15);
		}
		String result = smsService.getSMSBook(page, team, classTeam);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result);
		write.close();
		System.out.println("查询到的数据为：" + result);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getSMSCount")
	public void getSMSCount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int result = smsService.getSMSCount(user);
		System.out.println("一共发送了：" + result);
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write("" + result);
		write.close();
	}
}